package org.come.summonequip;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingConstants;

import org.come.Frame.ZhuFrame;
import org.come.bean.BuyShopBean;
import org.come.bean.QualityClBean;
import org.come.bean.SuitOperBean;
import org.come.entity.Goodstable;
import org.come.entity.PartJade;
import org.come.model.Shop;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;

import com.tool.btn.BaptizeBtn;
import com.tool.btn.MoBanBtn;
import com.tool.role.RoleData;

public class BtnSummonEquipMain extends MoBanBtn {

    /**
     * 1培养菜单按钮<br>
     * 2重铸菜单按钮<br>
     * 3觉醒菜单按钮<br>
     * 11当前装备按钮<br>
     * 12操作按钮<br>
     * 13兑换奖励按钮<br>
     * 14数值最大按钮<br>
     * 20下拉滚动条按钮<br>
     * 21向左按钮<br>
     * 22向右按钮<br>
     * 30兑换按钮<br>
     * 40保留原技能按钮<br>
     * 41替换新技能按钮<br>
     */
    private int caozuo;

    private JpanelSummonEquipMain jpanelSummonEquipMain;

    private JpanelNowEquip jpanelNowEquip;
    private JpanelCashRewardsMain jpanelCashRewardsMain;
    private JpanelReclaimSkillMain jpanelReclaimSkillMain;

    public BtnSummonEquipMain(String iconpath, int type, String text, int caozuo,
                              JpanelSummonEquipMain jpanelSummonEquipMain) {
        super(iconpath, type);
        this.caozuo = caozuo;
        this.jpanelSummonEquipMain = jpanelSummonEquipMain;
    }

    public BtnSummonEquipMain(String iconpath, int type, Color[] colors, Font font, String text, int caozuo,
                              JpanelSummonEquipMain jpanelSummonEquipMain) {
        super(iconpath, type, colors);
        setText(text);
        setFont(font);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.caozuo = caozuo;
        this.jpanelSummonEquipMain = jpanelSummonEquipMain;
    }

    public BtnSummonEquipMain(String iconpath, int type, String text, int caozuo, JpanelNowEquip jpanelNowEquip) {
        super(iconpath, type);
        setText(text);
        this.caozuo = caozuo;
        this.jpanelNowEquip = jpanelNowEquip;
    }

    public BtnSummonEquipMain(String iconpath, int type, Color[] colors, Font font, String text, int caozuo,
                              JpanelCashRewardsMain jpanelCashRewardsMain) {
        super(iconpath, type, colors);
        setText(text);
        setFont(font);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.caozuo = caozuo;
        this.jpanelCashRewardsMain = jpanelCashRewardsMain;
    }

    public BtnSummonEquipMain(String iconpath, int type, Color[] colors, Font font, String text, int caozuo,
                              JpanelReclaimSkillMain jpanelReclaimSkillMain) {
        super(iconpath, type, colors);
        setText(text);
        this.caozuo = caozuo;
        setFont(font);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.jpanelReclaimSkillMain = jpanelReclaimSkillMain;
    }









    @Override
    public void chooseyes() {
    }

    @Override
    public void chooseno() {
    }

    @Override
    public void nochoose(MouseEvent e) {
        if (caozuo <= 10) {
            jpanelSummonEquipMain.changeMenuView(caozuo);
        } else if (caozuo == 11) {
            if (!jpanelSummonEquipMain.getJpanelNowEquip().isVisible()) {
                jpanelSummonEquipMain.getpaneMessage();
            }
            // 展示召唤兽穿的装备
            jpanelSummonEquipMain.getJpanelNowEquip()
                    .setVisible(!jpanelSummonEquipMain.getJpanelNowEquip().isVisible());
            jpanelSummonEquipMain.getScrollPane().setVisible(false);
        } else if (caozuo == 12) {

            if (jpanelSummonEquipMain.getChooseEquip() == null) {
                ZhuFrame.getZhuJpanel().addPrompt2("请选择召唤兽装备");
                return;
            }
            Goodstable chooseEquip = GoodsListFromServerUntil.getRgid(jpanelSummonEquipMain.getChooseEquip());
            if (chooseEquip == null) {
                // 没有选中召唤兽装备
                ZhuFrame.getZhuJpanel().addPrompt2("没有选中召唤兽装备");
                return;
            }
            // 召唤兽装备属性
            String[] values = chooseEquip.getValue().split("\\|");
            SuitOperBean suitOperBean = new SuitOperBean();
            if (jpanelSummonEquipMain.getSummonEquipMenuType() == 1 && jpanelSummonEquipMain.getRadioBtnType() == 1) {

                /** 培养 */
                if (jpanelSummonEquipMain.getChooseGoods() == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有选择千年魂石");
                    return;
                }
                Goodstable chooseGoods = GoodsListFromServerUntil.getRgid(jpanelSummonEquipMain.getChooseGoods());
                if (chooseGoods == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有选中玄铁晶石");
                    return;
                }
                if (RoleData.getRoleData().getLoginResult().getGold().compareTo(jpanelSummonEquipMain.getMoney()) < 0) {
                    // 金钱不足
                    ZhuFrame.getZhuJpanel().addPrompt2("大话币不足10万");
                    return;
                }
                int valuesMessage = Integer.parseInt(jpanelSummonEquipMain.getValuesMessage(values, "通灵"));
                if (valuesMessage >= 6000) {
                    ZhuFrame.getZhuJpanel().addPrompt2("该装备的通灵值已经抵达上限，无法在培养");
                    return;
                }

                suitOperBean.setType(41);
                List<BigDecimal> goods = new ArrayList<>();
                // 添加物品rgid
                goods.add(chooseEquip.getRgid());
                goods.add(chooseGoods.getRgid());

                suitOperBean.setGoods(goods);
                deductGoods(chooseGoods, 1);
                // jpanelSummonEquipMain.recoverEquipImgTwo();
                if (chooseGoods.getUsetime() <= 0) {
                    jpanelSummonEquipMain.recoverEquipImgTwo();
                }
            } else if (jpanelSummonEquipMain.getSummonEquipMenuType() == 1
                    && jpanelSummonEquipMain.getRadioBtnType() == 2) {
                /** 分解 */
                suitOperBean.setType(42);
                List<BigDecimal> goods = new ArrayList<>();
                // 添加物品rgid
                goods.add(chooseEquip.getRgid());
                deductGoods(chooseEquip, 1);
                suitOperBean.setGoods(goods);
                jpanelSummonEquipMain.recoverEquipImgOne();
            } else if (jpanelSummonEquipMain.getSummonEquipMenuType() == 2
                    && jpanelSummonEquipMain.getRadioBtnType() == 1) {
                /** 重洗技能 */
                if (RoleData.getRoleData().getLoginResult().getGold().compareTo(jpanelSummonEquipMain.getMoney()) < 0) {
                    // 金钱不足
                    ZhuFrame.getZhuJpanel().addPrompt2("大话币不足10万");
                    return;
                }
                suitOperBean.setType(43);
                List<BigDecimal> goods = new ArrayList<>();
                // 添加物品rgid
                goods.add(chooseEquip.getRgid());
                Goodstable[] chooseWashGoods = jpanelSummonEquipMain.getChooseWashGoods();
                for (int i = 0; i < chooseWashGoods.length; i++) {
                    if (chooseWashGoods[i] == null) {
                        if (i == 0) {
                            ZhuFrame.getZhuJpanel().addPrompt2("请放入玄铁晶石");
                        } else if (i == 1) {
                            ZhuFrame.getZhuJpanel().addPrompt2("请放入内丹精华");
                        } else {
                            ZhuFrame.getZhuJpanel().addPrompt2("材料不齐全");
                        }
                        return;
                    }
                    goods.add(chooseWashGoods[i].getRgid());
                }
                suitOperBean.setGoods(goods);
                for (int i = 0; i < chooseWashGoods.length; i++) {
                    deductGoods(chooseWashGoods[i], 1);
                }
//                jpanelSummonEquipMain.recoverWashGoodsLabs(-1);
            } else if (jpanelSummonEquipMain.getSummonEquipMenuType() == 2
                    && jpanelSummonEquipMain.getRadioBtnType() == 2) {
                /** 重悟技能 */
                if (RoleData.getRoleData().getLoginResult().getGold().compareTo(jpanelSummonEquipMain.getMoney()) < 0) {
                    // 金钱不足
                    ZhuFrame.getZhuJpanel().addPrompt2("大话币不足500万");
                    return;
                }
                if (jpanelSummonEquipMain.getChooseGoods() == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有选择千年魂石");
                    return;
                }
                Goodstable chooseGoods = GoodsListFromServerUntil.getRgid(jpanelSummonEquipMain.getChooseGoods());
                if (chooseGoods == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有选中隐月神石");
                    return;
                }
                if (jpanelSummonEquipMain.getValuesMessage(values, "觉醒技") == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("该装备没有觉醒技");
                    return;
                }
                suitOperBean.setType(44);
                List<BigDecimal> goods = new ArrayList<>();
                // 添加物品rgid
                goods.add(chooseEquip.getRgid());
                goods.add(chooseGoods.getRgid());
                suitOperBean.setGoods(goods);
                deductGoods(chooseGoods, 1);
                if (chooseGoods.getUsetime() <= 0) {
                    jpanelSummonEquipMain.recoverEquipImgTwo();
                }
                // jpanelSummonEquipMain.recoverEquipImgTwo();
                // FormsManagement.showForm(94);
            } else if (jpanelSummonEquipMain.getSummonEquipMenuType() == 3
                    && jpanelSummonEquipMain.getRadioBtnType() == 1) {
                /** 提升等级 */
                long num = Long.parseLong(jpanelSummonEquipMain.getNumText().getText());
                if (num <= 0) {
                    return;
                }
                String valuesMessage = jpanelSummonEquipMain.getValuesMessage(values, "觉醒技");
                if (valuesMessage == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("该装备没有觉醒技");
                    return;
                }
                if (new BigDecimal(num).compareTo(RoleData.getRoleData().getLoginResult().getScoretype("比斗奖章")) > 0) {
                    // 比斗奖章数量不足
                    ZhuFrame.getZhuJpanel().addPrompt2("比斗奖章数量不足");
                    return;
                }
                String[] split = valuesMessage.split("&");
                long exp = Long.parseLong(split[3]);
                if (JpanelSummonEquipMain.expChangeLevel(exp) >= 20) {
                    ZhuFrame.getZhuJpanel().addPrompt2("等级已经达到最大值");
                    return;
                }
                if (RoleData.getRoleData().getLoginResult().getGold().compareTo(jpanelSummonEquipMain.getMoney()) < 0) {
                    // 金钱不足
                    ZhuFrame.getZhuJpanel().addPrompt2("大话币不足");
                    return;
                }

                suitOperBean.setType(46);
                suitOperBean.setJade(new PartJade((int) num, 0));
                List<BigDecimal> goods = new ArrayList<>();
                // 添加物品rgid
                goods.add(chooseEquip.getRgid());
                suitOperBean.setGoods(goods);
                jpanelSummonEquipMain.recoverEquipImgTwo();

            } else if (jpanelSummonEquipMain.getSummonEquipMenuType() == 3
                    && jpanelSummonEquipMain.getRadioBtnType() == 2) {
                /** 开启觉醒 */
                if (jpanelSummonEquipMain.getValuesMessage(values, "觉醒技") != null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("该装备已有觉醒技");
                    return;
                }
                if (RoleData.getRoleData().getLoginResult().getGold().compareTo(jpanelSummonEquipMain.getMoney()) < 0) {
                    // 金钱不足
                    ZhuFrame.getZhuJpanel().addPrompt2("大话币不足");
                    return;
                }
                if (jpanelSummonEquipMain.getChooseGoods() == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有选择千年魂石");
                    return;
                }
                Goodstable chooseGoods = GoodsListFromServerUntil.getRgid(jpanelSummonEquipMain.getChooseGoods());
                if (chooseGoods == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有选择千年魂石");
                    return;
                }
                suitOperBean.setType(45);
                List<BigDecimal> goods = new ArrayList<>();
                // 添加物品rgid
                goods.add(chooseEquip.getRgid());
                goods.add(chooseGoods.getRgid());
                suitOperBean.setGoods(goods);
                deductGoods(chooseGoods, 1);
                // jpanelSummonEquipMain.recoverEquipImgTwo();
                if (chooseGoods.getUsetime() <= 0) {
                    jpanelSummonEquipMain.recoverEquipImgTwo();
                }
            }
            String sendmes = Agreement.suitOperateAgreement(GsonUtil.getGsonUtil().getgson().toJson(suitOperBean));
            SendMessageUntil.toServer(sendmes);

        } else if (caozuo == 13) {
            JframeCashRewardsMain.getJframeCashRewardsMain().getJpanelCashRewardsMain().setGoodsType(1);
            FormsManagement.showForm(92);
        } else if (caozuo == 14) {
            jpanelSummonEquipMain.getNumText().setText(
                    RoleData.getRoleData().getLoginResult().getScoretype("比斗奖章").toString());
        } else if (caozuo == 15) {
            FormsManagement.showForm(93);
        } else if (caozuo == 20) {
            jpanelSummonEquipMain.getScrollPane().setVisible(!jpanelSummonEquipMain.getScrollPane().isVisible());
        } else if (caozuo == 21) {
            if (jpanelNowEquip.getNowPage() <= 1) {
                return;
            }
            jpanelNowEquip.setNowPage(jpanelNowEquip.getNowPage() - 1);
        } else if (caozuo == 22) {
            if ((jpanelNowEquip.getNowPage()) * 12 > jpanelNowEquip.getSummonEquipList().size()) {
                return;
            }
            jpanelNowEquip.setNowPage(jpanelNowEquip.getNowPage() + 1);
        } else if (caozuo == 30) {
            Shop chooseShop = jpanelCashRewardsMain.getChooseShop();
            if (chooseShop == null) {
                ZhuFrame.getZhuJpanel().addPrompt2("请选择要兑换的物品");
                return;
            }
            // 购买数量
            int parseInt = Integer.parseInt(jpanelCashRewardsMain.getTextNum().getText() == "" ? "0"
                    : jpanelCashRewardsMain.getTextNum().getText());
            if (parseInt <= 0) {
                ZhuFrame.getZhuJpanel().addPrompt2("兑换数量不能为0");
                return;
            }
            int sum = GoodsListFromServerUntil.Surplussum(chooseShop.getShoptype()+"", chooseShop.getShopid(), parseInt);
            if (sum <= 0) {
                ZhuFrame.getZhuJpanel().addPrompt2("背包空间不足");
                return;
            }
            // 价格
            BigDecimal shopPrice = jpanelCashRewardsMain.getShopPrice();
            if (jpanelCashRewardsMain.getGoodsType() == 1) {
                if (shopPrice.compareTo(RoleData.getRoleData().getLoginResult().getScoretype("比斗奖章")) > 0) {
                    ZhuFrame.getZhuJpanel().addPrompt2("比斗奖章不足");
                    return;
                }
            } else if (jpanelCashRewardsMain.getGoodsType() == 2) {
                if (shopPrice.compareTo(RoleData.getRoleData().getLoginResult().getScoretype("星芒")) > 0) {
                    ZhuFrame.getZhuJpanel().addPrompt2("星芒不足");
                    return;
                }
            }else if (jpanelCashRewardsMain.getGoodsType() == 3) {
                if (shopPrice.compareTo(RoleData.getRoleData().getLoginResult().getScoretype("勇者积分")) > 0) {
                    ZhuFrame.getZhuJpanel().addPrompt2("勇者积分不足");
                    return;
                }
            }
            BuyShopBean bean = new BuyShopBean();
            bean.setAte(1);
            bean.setCd(chooseShop.getShopid());
            bean.setSum(parseInt);
            String senmes = Agreement.getAgreement().nbuyAgreement(GsonUtil.getGsonUtil().getgson().toJson(bean));
            SendMessageUntil.toServer(senmes);

        } else if (caozuo == 40) {
            FormsManagement.HideForm(94);
            jpanelReclaimSkillMain.setQualityClBean(null);
        } else if (caozuo == 41) {
            QualityClBean qualityClBean = jpanelReclaimSkillMain.getQualityClBean();
            if (qualityClBean == null) {
                return;
            }
            Goodstable goodstable = GoodsListFromServerUntil.getRgid(JframeSummonEquipMain.getJframeSummonEquipMain()
                    .getJpanelSummonEquipMain().getChooseEquip());
            if (goodstable == null) {
                return;
            }
            goodstable.setValue(BaptizeBtn.newExtra(goodstable.getValue().split("\\|"), 5, qualityClBean.getNewAttr()));
            String senmes = Agreement.extrAttrOperAgreement(GsonUtil.getGsonUtil().getgson().toJson(qualityClBean));
            SendMessageUntil.toServer(senmes);
            jpanelReclaimSkillMain.setQualityClBean(null);
            FormsManagement.HideForm(94);
        }
    }

    /** 物品消耗扣除 */
    public void deductGoods(Goodstable goodstable, int num) {
        goodstable.goodxh(num);
        if (goodstable.getUsetime() <= 0) {
            GoodsListFromServerUntil.Deletebiaoid(goodstable.getRgid());
        }
    }

}
